/*
 *	Copyright (C) 2008 - Mauricio Bieze Stefani
 *	This file is part of the Insano Game Engine.
 *
 *	Insano is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Insano is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Insano.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ClientConfig.h"

ClientConfig config;

#include "videosetupMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(videosetupFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(videosetupFrame)
const long videosetupFrame::ID_STATICTEXT1 = wxNewId();
const long videosetupFrame::ID_CHOICE3 = wxNewId();
const long videosetupFrame::ID_CHECKBOX1 = wxNewId();
const long videosetupFrame::ID_CHOICE1 = wxNewId();
const long videosetupFrame::ID_STATICTEXT3 = wxNewId();
const long videosetupFrame::ID_CHOICE2 = wxNewId();
const long videosetupFrame::ID_BUTTON3 = wxNewId();
const long videosetupFrame::ID_BUTTON4 = wxNewId();
//*)

BEGIN_EVENT_TABLE(videosetupFrame,wxFrame)
    //(*EventTable(videosetupFrame)
    //*)
END_EVENT_TABLE()

videosetupFrame::videosetupFrame(wxWindow* parent,wxWindowID id)
{
	config.load("client.xml");
    //(*Initialize(videosetupFrame)
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer5;
    wxBoxSizer* BoxSizer2;
    wxBoxSizer* BoxSizer1;
    wxBoxSizer* BoxSizer3;

    Create(parent, wxID_ANY, _("Video Setup Tool"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Video driver:"), wxDefaultPosition, wxSize(70,23), 0, _T("ID_STATICTEXT1"));
    BoxSizer2->Add(StaticText1, 0, wxALL|wxSHAPED|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    Choice3 = new wxChoice(this, ID_CHOICE3, wxDefaultPosition, wxSize(108,21), 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE3"));
    Choice3->SetSelection( Choice3->Append(_("DirectX 9.0c")) );
    Choice3->Append(_("OpenGL 1.5"));
    Choice3->Append(_("Irrlicht Software"));
    Choice3->Append(_("Burnings Software"));
    Choice3->Append(_("Null"));
    BoxSizer2->Add(Choice3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer2, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    CheckBox1 = new wxCheckBox(this, ID_CHECKBOX1, _("Fullscreen"), wxDefaultPosition, wxSize(69,13), 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
    CheckBox1->SetValue(false);
    BoxSizer3->Add(CheckBox1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Choice1 = new wxChoice(this, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
    Choice1->SetSelection( Choice1->Append(_("16bpp")) );
    Choice1->Append(_("24bpp"));
    Choice1->Append(_("32bpp"));
    BoxSizer3->Add(Choice1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer3, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    StaticText3 = new wxStaticText(this, ID_STATICTEXT3, _("Resolution:"), wxDefaultPosition, wxSize(67,13), 0, _T("ID_STATICTEXT3"));
    BoxSizer4->Add(StaticText3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Choice2 = new wxChoice(this, ID_CHOICE2, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE2"));
    Choice2->SetSelection( Choice2->Append(_("800x600")) );
    Choice2->Append(_("800x480"));
    Choice2->Append(_("1024x768"));
    Choice2->Append(_("1152x864"));
    Choice2->Append(_("1280x800"));
    Choice2->Append(_("1280x1024"));
    Choice2->Append(_("1440x900"));
    BoxSizer4->Add(Choice2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer4, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    Button3 = new wxButton(this, ID_BUTTON3, _("OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
    BoxSizer5->Add(Button3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Button4 = new wxButton(this, ID_BUTTON4, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON4"));
    BoxSizer5->Add(Button4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer5, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(BoxSizer1);
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);
    Center();

    Connect(ID_CHECKBOX1,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&videosetupFrame::OnCheckBox1Click);
    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&videosetupFrame::OnButton3Click);
    Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&videosetupFrame::OnButton4Click);
    //*)

	if(config.video.bpp==24)
		Choice1->SetSelection(1);
	else if(config.video.bpp==32)
		Choice1->SetSelection(2);
	else // (bpp==16) or ...
		Choice1->SetSelection(0);

	if((config.video.horizontal==800)&&(config.video.vertical==480))
		Choice2->SetSelection(1);
	else if(config.video.horizontal==1024)
		Choice2->SetSelection(2);
	else if(config.video.horizontal==1152)
		Choice2->SetSelection(3);
	else if((config.video.horizontal==1280)&&(config.video.vertical==800))
		Choice2->SetSelection(4);
	else if((config.video.horizontal==1280)&&(config.video.vertical==1024))
		Choice2->SetSelection(5);
	else if(config.video.horizontal==1440)
		Choice2->SetSelection(6);
	else // (config.video.horizontal==800) or ...
		Choice2->SetSelection(0);

	if(config.video.driver==irr::video::EDT_DIRECT3D9)
		Choice3->SetSelection(0);
	else if(config.video.driver==irr::video::EDT_OPENGL)
		Choice3->SetSelection(1);
	else if(config.video.driver==irr::video::EDT_SOFTWARE)
		Choice3->SetSelection(2);
	else if(config.video.driver==irr::video::EDT_BURNINGSVIDEO)
		Choice3->SetSelection(3);
	else if(config.video.driver==irr::video::EDT_NULL)
		Choice3->SetSelection(4);

	CheckBox1->SetValue(config.video.fullscreen);
	config.video.fullscreen ? Choice1->Enable() : Choice1->Disable();
}

videosetupFrame::~videosetupFrame()
{
    //(*Destroy(videosetupFrame)
    //*)
}

void videosetupFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void videosetupFrame::OnButton3Click(wxCommandEvent& event)
{
	int c1=Choice1->GetSelection();
	if(c1==0) config.video.bpp=16;
	else if(c1==1) config.video.bpp=24;
	else if(c1==2) config.video.bpp=32;

	int c2=Choice2->GetSelection();
	if(c2==0)
	{
		config.video.horizontal=800;
		config.video.vertical=600;
	}
	else if(c2==1)
	{
		config.video.horizontal=800;
		config.video.vertical=480;
	}
	else if(c2==2)
	{
		config.video.horizontal=1024;
		config.video.vertical=768;
	}
	else if(c2==3)
	{
		config.video.horizontal=1152;
		config.video.vertical=864;
	}
	else if(c2==4)
	{
		config.video.horizontal=1280;
		config.video.vertical=800;
	}
	else if(c2==5)
	{
		config.video.horizontal=1280;
		config.video.vertical=1024;
	}
	else if(c2==6)
	{
		config.video.horizontal=1440;
		config.video.vertical=900;
	}

	int c3=Choice3->GetSelection();
	if(c3==0) config.video.driver=irr::video::EDT_DIRECT3D9;
	else if(c3==1) config.video.driver=irr::video::EDT_OPENGL;
	else if(c3==2) config.video.driver=irr::video::EDT_SOFTWARE;
	else if(c3==3) config.video.driver=irr::video::EDT_BURNINGSVIDEO;
	else if(c3==4) config.video.driver=irr::video::EDT_NULL;

	config.video.fullscreen=CheckBox1->GetValue();

	config.save("client.xml");
	Close();
}

void videosetupFrame::OnCheckBox1Click(wxCommandEvent& event)
{
	CheckBox1->GetValue() ? Choice1->Enable() : Choice1->Disable();
}

void videosetupFrame::OnButton4Click(wxCommandEvent& event)
{
	Close();
}
